package com.bort.system.controller.client;

import com.bort.system.common.DataContainer;
import com.bort.system.info.DeskInfo;
import com.bort.system.model.TGameLog;
import com.bort.system.service.TDeskService;
import com.bort.system.utils.SpringContextUtils;
import de.felixroske.jfxsupport.FXMLController;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

@FXMLController
public class DialogController implements Initializable {

    DeskInfo data;

    TDeskService deskService = null;

    ToggleGroup toggleGroup;

    List<Integer> hours = null;
    List<Integer> minutes = null;

    @FXML
    RadioButton timeLimit;

    @FXML
    RadioButton timeUnLimit;

    @FXML
    AnchorPane anchor;

    @FXML
    ComboBox<Integer> hour;

    @FXML
    ComboBox<Integer> minute;

    @FXML
    TextArea player;

    @FXML
    Button confirmBtn;

    @FXML
    void onLimitClick(ActionEvent event){
        selectTimeLimit(1);
    }

    @FXML
    void onUnLimitClick(ActionEvent event){
        selectTimeLimit(2);
    }

    @FXML
    void onConfirmBtnClick(ActionEvent event){
        Integer hValue = hour.getValue();
        Integer mValue = minute.getValue();
        Integer zero = new Integer(0);
        if(zero.equals(hValue)  && zero.equals(mValue)){

        }else{
            TGameLog gameLog = deskService.openDesk(data,hValue,mValue,player.getText());
            if(gameLog != null){
                Scene scene = anchor.getScene();
                Stage window = (Stage)scene.getWindow();
                window.close();
                DeskController deskCtrl = DataContainer.deskCtrlMap.get(data.getId());
                deskCtrl.deskOpen(gameLog);
            }
        }
    }

    @FXML
    void onCancelBtnClick(ActionEvent event){
        Scene scene = anchor.getScene();
        Stage window = (Stage)scene.getWindow();
        window.close();
    }

    @FXML
    void onHourSelect(ActionEvent event){
        updateBtnState();
    }

    @FXML
    void onMinuteSelect(ActionEvent event){
        updateBtnState();
    }

    private void updateBtnState(){
        Integer zero = new Integer(0);
        Integer value = hour.getValue();
        Integer mValue = minute.getValue();
        if(zero.equals(value) && zero.equals(mValue)){
            confirmBtn.setDisable(true);
        }else{
            confirmBtn.setDisable(false);
        }
    }


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        System.out.println("Loading...");
        data = DataContainer.deskContainer.get(DataContainer.selectDeskId);
        deskService = SpringContextUtils.getBean(TDeskService.class);
        toggleGroup = new ToggleGroup();
        timeUnLimit.setToggleGroup(toggleGroup);
        timeLimit.setToggleGroup(toggleGroup);
        toggleGroup.selectToggle(timeUnLimit);
        selectTimeLimit(2);
        hours = new ArrayList<>();
        for(int i = 0;i<25;i++){
            hours.add(i);
        }
        ObservableList<Integer> options = FXCollections.observableList(hours);
        hour.setItems(options);
        minutes = new ArrayList<>();
        for (int i=0;i<60;i++){
            minutes.add(i);
        }
        ObservableList<Integer> minOptions = FXCollections.observableList(minutes);
        minute.setItems(minOptions);
        hour.setValue(1);
        minute.setValue(0);
    }



    void selectTimeLimit(int type){
        data.setTimeLimit(type);
        if(type == 1){
            hour.setDisable(false);
            minute.setDisable(false);
        }else{
            hour.setDisable(true);
            minute.setDisable(true);
        }
    }


}
